<?php
/**
 * 团购抢购
 *
 * by abc.com 多用户商城
 *
 *
 */
//by abc.com
//use Shopnc\Tpl;

defined('InShopNC') or exit('Access Invalid!');
class groupbuyControl extends mobileHomeControl{
	public function __construct() {
        parent::__construct();
    }

    //即将开团的团购列表
    public function groupbuy_soon_listOp(){
    	$data=$this->getlist('团购','soon');
    	output_data(array('groupbuy_soon_list'=>$data));
    }

    //正在团购的团购列表
    public function groupbuy_listOp(){
    	$data=$this->getlist('团购','now');
    	output_data(array('groupbuy_list'=>$data));
    }
    
    /*
     * 20170105周家财新增
     * 显示团购活动列表
     */
    public function show_groupbuy_listOp(){
        $groupbuy=$this->getlist('团购','now');   
        //把团长价和团员价加到数据中
        $groupbuy_team_template_model=Model('groupbuy_team_template');
       
        $temp_groupbuy=array();
        //把商品小图传上来，同时减少字段
        $goods_model=Model('goods');//getGoodsInfo
        foreach ($groupbuy as $key => $val){
            $goods_info = Model('goods')->getGoodsOnlineInfoAndPromotionById($val['goods_id']);
            if(empty($goods_info)){
                unset($groupbuy[$key]);
            }
        }
        
        $groupbuy=array_merge($groupbuy);
        $model_goods=Model('goods');
        foreach ($groupbuy as $key => $val){
            $temp_groupbuy[$key]['groupbuy_id']=$groupbuy[$key]['groupbuy_id'];
            $team_template=$groupbuy_team_template_model->get_template($temp_groupbuy[$key]);
            $temp_groupbuy[$key]['groupbuy_lead_price']=$team_template['team_template_lead_price'];
            $temp_groupbuy[$key]['groupbuy_member_price']=$team_template['team_template_member_price'];
            $temp_groupbuy[$key]['groupbuy_team_num']=$team_template['team_template_num'];
            $temp_groupbuy[$key]['goods_id']=$groupbuy[$key]['goods_id'];
            $temp_groupbuy[$key]['goods_name']=$groupbuy[$key]['goods_name'];
            $con['goods_id']=$groupbuy[$key]['goods_id'];
            $goods=$model_goods->getGoodsInfo($con);
            //要去common表找
            $common['goods_commonid']=$goods['goods_commonid'];
            $common_info=$model_goods->getGoodeCommonInfo($common);
            $temp_groupbuy[$key]['goods_name']=$common_info['goods_name'];
            
            $temp_groupbuy[$key]['goods_price']=$groupbuy[$key]['goods_price'];
            $temp_groupbuy[$key]['count_down']=$groupbuy[$key]['count_down'];
            $temp_groupbuy[$key]['groupbuy_id']=$groupbuy[$key]['groupbuy_id'];
            
            $condition['goods_id']=$groupbuy[$key]['goods_id'];
            $goods_info=$goods_model->getGoodsDetail($condition['goods_id']);
            $temp_groupbuy[$key]['goods_img']=$goods_info['goods_image_mobile'];
        }

        output_data(array('groupbuy_list'=>$temp_groupbuy));
        
    }
    
    //20170110周家财新增 S
      /*
     * 该团购商品下的所有拼团
     */
    
    public function team_listOp(){
        $condition['groupbuy_id']=$_REQUEST['groupbuy_id'];
        $condition['pay_status']=array('gt',0);
        $limit=0;
        $groupbuy_team_model=Model('groupbuy_team');
        $team_list=$groupbuy_team_model->get_team_list($limit,$condition);
        $member_model=Model('member');
        $condition=array();
        foreach ($team_list as $key =>$val){
            $condition['member_id']=$val['lead_id'];
            $member_info=$member_model->getMemberInfo($condition);
            if (!empty($member_info['member_avatar'])){
                    $team_list[$key]['lead_avatar']=getMemberAvatarForFilePath($member_info['member_avatar']);
                }else{
                    $team_list[$key]['lead_avatar']=UPLOAD_SITE_URL.'/'.ATTACH_AVATAR.'/default_avatar.png';
                }
            }
        
        
        output_data($team_list);
    }
    
    
    
    /*
     *拼团跑马灯 
     */
    public function cycmem_listOp(){
        $cycmem_list=$this->creatcyc_list();
        output_data($cycmem_list);
        
    }
    /**
     * 创建参团数据
     */
    public function creatcyc_list(){
        $groupbuy_model=Model('groupbuy');
        $condition['start_time']=array('lt', TIMESTAMP);
        $condition['end_time']=array('gt', TIMESTAMP);
        $group_list=$groupbuy_model->getGroupbuyList($condition);
        //把team表数据找出来
        $groupbuy_mem_model=Model('groupbuy_mem');
        $temp_cyc=array();
        $cyc_list=array();
        if ($group_list!=null && !empty($group_list)){
            foreach ($group_list as $key =>$val){
                $temp_cyc[$key]=$groupbuy_mem_model->GetCycleMem($val['groupbuy_id']);
            }
            $i=0;
            foreach ($temp_cyc as $key_cyc =>$val_cyc){
                if ($val_cyc != null && !empty($val_cyc)){
                    foreach ($val_cyc as $key_1 =>$val_1){
                        $cyc_list[$i]=$val_1;
                        $i++;
                    }
                }
            }
            
            if($cyc_list !=null && !empty($cyc_list)){
                foreach ($cyc_list as $key =>$val){
                    $condition_grop['groupbuy_id']=$val['groupbuy_id'];
                    $group_info=$groupbuy_model->getGroupbuyOnlineInfo($condition_grop);
                    $cyc_list[$key]['goods_short_name']=$group_info['short_name'];
                }
                $cyc_list=array_slice($cyc_list, 0, 3);
            }else{
                $cyc_list=array('status'=>'error','msg'=>'无数据');
            }
            
            
        }else{
            $cyc_list=array('status'=>'error','msg'=>'无数据');
        }
        
        return ($cyc_list);
    }
    
    /*
     * 实时跑马灯
     */
    public function ontime_cycmemOp(){
       $cycmem_list=$this->creatcyc_list();
       $condition['payment_time']=array(array('gt',TIMESTAMP-20),array('lt',TIMESTAMP),'and');
       $condition['team_id']=array('gt',0);
       //去订单表找array(array('egt', 20),array('eq',0),'or');
       $order_model=Model('order');
       $order_info=$order_model->getOrderInfo($condition);   
       if($order_info !=null && !empty($order_info)){
           //要得到team_id,
           $team_id=$order_info['team_id'];
           $ontime_cycmem=array();
           foreach ($cycmem_list as $key =>$val){
               if ($val['team_id']==$team_id){
                   $ontime_cycmem=$cycmem_list[$key];
                   break;
               }
           }
           if($ontime_cycmem !=null && !empty($ontime_cycmem)){
               output_data($ontime_cycmem);
           }else{
               output_data(array('status'=>'error','msg'=>'无数据'));
           }
       }else{
           output_data(array('status'=>'error','msg'=>'无数据'));
       }
       
       
    }
    
    
    public function wapteam_listOp(){
        $condition['groupbuy_id']=$_REQUEST['groupbuy_id'];
        $condition['pay_status']=array('gt',0);
        $limit=0;
        $groupbuy_team_model=Model('groupbuy_team');
        $team_list=$groupbuy_team_model->get_team_list($limit,$condition);
        $member_model=Model('member');
        $condition=array();
        foreach ($team_list as $key =>$val){
            $team_list[$key]['lead_name']=substr_replace($val['lead_name'],"****",3,4);
            $condition['member_id']=$val['lead_id'];
            $member_info=$member_model->getMemberInfo($condition);
            if (!empty($member_info['member_avatar'])){
                $team_list[$key]['lead_avatar']=getMemberAvatarForFilePath($member_info['member_avatar']);
            }else{
                $team_list[$key]['lead_avatar']=UPLOAD_SITE_URL.'/'.ATTACH_AVATAR.'/default_avatar.png';
            }
        }
        $condition=array();
        $condition['groupbuy_id']=$_REQUEST['groupbuy_id'];
        $condition['goods_id']=$_REQUEST['goods_id'];
        $model_group_team_template = Model('groupbuy_team_template');
        $template = $model_group_team_template->get_template($condition);
        
        $model_goods = Model('goods');
        $condition=array();
        $goods_detail = $model_goods->getGoodsDetail($_REQUEST['goods_id']);
        $goods_detail['goods_image']=UPLOAD_SITE_URL.'/shop/store/goods/1/'.$goods_detail['goods_image'];
    
        output_data(array('team_list' => $team_list,'template' => $template,'goods' => $goods_detail));
    }
    
    
    //20170110周家财新增 E
     
    //20170110周家财新增S
    /*
     * 该拼团下的所有团员列表
     */
    public function team_memberOp(){
        $condition['team_id']=$_POST['team_id'];
        $condition['pay_status']=1;
        $groupbuy_mem_model=Model('groupbuy_mem');
        $result=$groupbuy_mem_model->member_list($condition);
        output_data(array('member_list'=>$result));
    }
    
    //20170110周家财新增E
    
    
    /**
     * 判断拼团商品库存
     */
    public function check_group_numOp(){
        //商品详情
        if (empty($_REQUEST['goods_id'])  || empty($_REQUEST['groupbuy_id'])){
            output_data(array('status'=>'error','msg'=>'参数不足'));
        }
         
        $goods_id=intval($_REQUEST['goods_id']);
        $goods_model=Model('goods');
        $goods_info=$goods_model->getGoodsDetail($goods_id);
        
        if ($goods_info['goods_info']['goods_storage'] <=3){
            output_data(array('status'=>'error','msg'=>'商品库存不足'));
        }else{
            output_data(array('status'=>'success','msg'=>'OK'));
        }
    }
    
    //20170105周家财修改S
    /*
     * 团购商品详情
     */
    public function groupbuy_goods_detailOp(){
       
        //商品详情
        if (empty($_REQUEST['groupbuy_id'])){
            output_data(array('status'=>'error','msg'=>'参数不足'));
        }
        $model_groupbuy=Model('groupbuy');
        $group['groupbuy_id']=$_REQUEST['groupbuy_id'];
        $group_info=$model_groupbuy->getGroupbuyOnlineInfo($group);
        
        $goods_id=intval($_REQUEST['goods_id']?$_REQUEST['goods_id']:$group_info['goods_id']);
        $goods_model=Model('goods');
        $goods_info=$goods_model->getGoodsDetail($goods_id);

        if ($goods_info['goods_info']['goods_storage'] <=3){
            output_data(array('status'=>'error','msg'=>'商品库存不足'));
        }
        
    	//添加拼团商品分享URL
    	$goods_info['groupbuy_url']=urlShop('groupbuy','groupbuy_goods_detail',array('groupbuy_id' => $_REQUEST['groupbuy_id'],'goods_id'=>$_REQUEST['goods_id']));
    	$goods_info['groupbuy_url']=str_replace('/shop/shop/', '/shop/mobile/', $goods_info['groupbuy_url']);
    	$goods_info['goods_info']['goods_short_name']=$group_info['short_name'];

        
        //店铺信息2017-01-23 S
        $model_store = Model('store');
        $store_info = $model_store->getStoreInfoByID($goods_info['goods_info']['store_id']);
        
        $goods_info['store_info']['store_id'] = $store_info['store_id'];
        //20161223 修改  ----店铺名称先修改为品牌名,后面还要改回来
        $goods_info['store_info']['store_name'] = $goods_info['goods_info']['store_name'];
        //$goods_detail['store_info']['store_name'] = $store_info['store_name'];
        $goods_info['store_info']['member_id'] = $store_info['member_id'];
        //显示QQ及旺旺 多用户商城
        $goods_info['store_info']['store_qq'] = $store_info['store_qq'];
        $goods_info['store_info']['store_ww'] = $store_info['store_ww'];
        $goods_info['store_info']['store_phone'] = $store_info['store_phone'];
        $goods_info['store_info']['member_name'] = $store_info['member_name'];
        
        $goods_info['store_info']['avatar'] = getMemberAvatarForID($store_info['member_id']);
        if(intval($goods_info['goods_info']['is_pick']) > 0) {
            $setting = Model("setting")->getListSetting();
            $goods_info['goods_info']['pick_address'] = $setting['delivery_area_info'].$setting['delivery_address'];
        }
        
        //店铺信息2017-01-23 E
        
        //查找所有拼图
        $groupbuy_team_model=Model('groupbuy_team');
        $condition_template['groupbuy_id']=$condition['groupbuy_id']=$_REQUEST['groupbuy_id'];
        $condition['end_time']=array('gt',TIMESTAMP);
        $condition['pay_status']=array('gt',0);
        $limit=2;
        $team_list=$groupbuy_team_model->get_team_list($limit,$condition);
        //查找用户头像

        //$member_info['avatar'] = getMemberAvatarForFilePath($this->member_info['member_avatar']);
        $member_model=Model('member');

        foreach ($team_list as $key_team =>$val_team){
            $team_list[$key_team]['lead_name']=substr_replace($val_team['lead_name'],"****",3,4);
            $member_id=$team_list[$key_team]['lead_id'];
            $member_info=$member_model->getMemberInfoByID($member_id);
            if (!empty($member_info['member_avatar'])){
                $team_list[$key_team]['lead_avatar']=getMemberAvatarForFilePath($member_info['member_avatar']);
            }else{                  //http://localhost/lxs/data/upload   //shop/avatar/
                $team_list[$key_team]['lead_avatar']=UPLOAD_SITE_URL.'/'.ATTACH_AVATAR.'/default_avatar.png';
            }
            
            $team_list[$key_team]['remain_times'] = $this->get_timediff($val_team['end_time'],$val_team['start_time']);
        }
        
        //查找到团长价和团员价
        $groupbuy_team_template_model=Model('groupbuy_team_template');
        $team_template=$groupbuy_team_template_model->get_template($condition_template);
        $goods_info['goods_info']['groupbuy_lead_price']=$team_template['team_template_lead_price'];
        $goods_info['goods_info']['groupbuy_member_price']=$team_template['team_template_member_price'];
        $goods_info['goods_info']['team_num']=$team_template['team_template_num'];
        $goods_info['goods_info']['share_url']="https://www.gxxwkj.com/shop/group_product.html?gid=".$_REQUEST['groupbuy_id'];
        
        $goods_info['team_list']=$team_list;

        //查找地区，只需要两级，第一级省，第二级市
        $model_area = Model('area');
        $condition_pri['area_deep']=1;
        $condition_pri['area_id']=array('lt', 32);
        $area_province = $model_area->getAreaList($condition_pri, 'area_id,area_name,area_parent_id');
        
        $condition_city['area_deep']=2;
        $condition_city['area_parent_id']=array('lt', 32);
        $area_city = $model_area->getAreaList($condition_city, 'area_id,area_name,area_parent_id');
        
        $goods_list = $goods_model->getGoodsOnlineList(array(), '*', 0, 'goods_id desc', 4, null, false);
        foreach ($goods_list as $key=>$val){
            //切割图片路径
            $img = explode(".", $val['goods_image']);//得到名称和后缀
            $goods_list[$key]['goods_image']=UPLOAD_SITE_URL.'/shop/store/goods/1/'.$val['goods_image'];
        }
        //查找common表
         $common['goods_commonid']=$goods_info['goods_info']['goods_commonid'];
         $common_info=$goods_model->getGoodeCommonInfo($common);

         $goods_info['goods_info']['goods_name']=$common_info['goods_name'];
       
         //保存用户浏览过的商品
        $key=$_REQUEST['key'];
        $model_mb_user_token = Model('mb_user_token');
        $mb_user_token_info = $model_mb_user_token->getMbUserTokenInfoByToken($key);
        $model_member = Model('member');
        $member_info = $model_member->getMemberInfoByID($mb_user_token_info['member_id']);
        $data['fever_goodsid']=$goods_info['goods_info']['goods_id'];
        $data['fever_goodsname']=$goods_info['goods_info']['goods_name'];
        $data['fever_goodsprice']=$team_template['team_template_lead_price'];
        $data['fever_actid']='';
        $data['fever_acttype']='2';
        $data['fever_actname']='拼团';
        $data['fever_actiontype']=1;
        $data['fever_actiontime']=TIMESTAMP;
        $data['client']=$_POST['client'];
        $data['fever_actionip']=$_SERVER["REMOTE_ADDR"];
        $data['fever_actionmobile']=$member_info['member_mobile'];
        $model_fever=Model('fever');
        $result=$model_fever->saveFever($data);    
        
        output_data($goods_info,array('area_province'=>$area_province,'area_city'=>$area_city,'goods_list'=>$goods_list));
        //output_data($goods_info);
    
    }
    //20170105修改E

  
    //即将开团的抢购列表
    public function panicbuy_soon_listOp(){
    	$data=$this->getlist('抢购','soon');
    	output_data(array('panicbuy_soon_list'=>$data));
    }

    //正在团购的抢购列表
    public function panicbuy_listOp(){
    	$data=$this->getlist('抢购','now');
    	output_data(array('panicbuy_list'=>$data));
    }
    /*
     * 团购活动详情
     */
    public function groupbuy_detailOp(){
    	$groupbuy_id=intval($_REQUEST['groupbuy_id']);
    	$groupbuy=Model('groupbuy');
    	//根据抢购编号读取抢购信息
    	$data=$groupbuy->getGroupbuyInfoByID($groupbuy_id);
    	output_data($data);
    }
   
    
    /**
     *
     * @param unknown $gc_name
     * @param unknown $type
     */
    public function getlist($gc_name,$type){
    	$g_class=new Model();
    	
    	//$g_c_id=$g_class->table('groupbuy_class')->where(array('class_name'=>'团购'))->find();
    	$g_c_id=$g_class->table('groupbuy_class')->getfby_class_name($gc_name,'class_id');
       
    	$groupbuy=Model('groupbuy');
    	$condition['class_id']=$g_c_id;
    	if($type=='soon'){
    	    //读取即将开始的抢购列表
    		$data=$groupbuy->getGroupbuySoonList($condition);

    	}
    	if($type=='now'){
    	    //读取当前可用的抢购列表
			$data=$groupbuy->getGroupbuyOnlineList($condition);
			foreach ($data as $key =>$value){
			     foreach ($value as $k=>$val){
			         if ($k == 'groupbuy_url'){
			             $data[$key]['groupbuy_url']=str_replace('/shop/', '/mobile/', $val);
			         }
			         if($k == 'goods_url'){
			             $data[$key]['goods_url']=str_replace('/shop/', '/mobile/', $val);
			         }
			     }
			}
    	}
      
        for($i=0;$i<count($data);$i++){
           
            $data[$i]['groupbuy_image1']=gthumb($data[$i]['groupbuy_image1'], 'max');
            $data[$i]['groupbuy_image']=gthumb($data[$i]['groupbuy_image'], 'max');
        }
       
    	return $data;
    }
    
    /**
     * 拼团详情
     */
    public function group_detailOp(){
        //用户信息，team_id,order_id
        
        $model_order = Model('order');
        $goods_model=Model('goods');
        $condition = array();
        $condition['order_id'] = $_REQUEST['order_id'];
        //$condition['order_state'] = array('gt',10);
        $condition['order_state']=array(array('egt', 20),array('eq',0),'or');
        
        $group=array();
        $order = $model_order -> getOrderInfo($condition,array('order_common','order_goods'));
        if($_REQUEST['team_id']>0 && !empty($_REQUEST['team_id']) && $order['team_id']==$_REQUEST['team_id']){
            $condition['team_id'] = $_REQUEST['team_id'];
        }else{
            output_error('拼团订单错误');
        }
        foreach ($order['extend_order_goods'] as $key=>$val){
            $order['extend_order_goods'][$key]['goods_image']=UPLOAD_SITE_URL.'/shop/store/goods/1/'.$val['goods_image'];
        }
        $group_goods=$order['extend_order_goods'][0];
         
        $group_goods['add_time_txt'] = date('Y-m-d H:i:s',$order['add_time']);
        $group_goods['pay_time_txt'] = date('Y-m-d H:i:s',$order['payment_time']);//付款时间
        //查找参团
//         $mem['member_id']=$this->member_info['member_id'];
        $mem['team_id']=$order['team_id'];
        $model_groupbuy_mem=Model('groupbuy_mem');
        $mem_info=$model_groupbuy_mem->groupbuy_mem_info($mem);
        if($mem_info !=null && !empty($mem_info)){
            $save=round(($group_goods['goods_price']-$mem_info['member_price']),2);
            $group['save']=$save;//拼团优惠
            $group['group_price']=$mem_info['member_price'];//拼团金额
            $team['team_id']=$order['team_id'];
            
            //查找拼团
            $model_groupbuy_team=Model('groupbuy_team');
            $team_info=$model_groupbuy_team->groupbuy_team_info($team);
            $group['group_number']=$team_info['team_number'];//拼团人数
            
            if(($team_info['team_number']-$team_info['record_num']>0) && ($team_info['end_time']>TIMESTAMP)){
                $group['need']= $team_info['team_number']-$team_info['record_num'];//还需几人成团
                $group['group_state']=0;
                $group['group_txt']="正在组团中";
            }else if(($team_info['team_number']-$team_info['record_num']=0) && ($team_info['end_time']>=TIMESTAMP)){
                $group['need']=0;//人数满足
                $group['group_state']=1;
                $group['group_txt']="拼团成功";
            }else if(($team_info['end_time']<=TIMESTAMP) && ($team_info['team_number']-$team_info['record_num']>0)){
                $group['need']=-1;//失败
                $group['group_state']=2;
                $group['group_err']="拼团失败";
            }
            $team['pay_status']=1;
            $memList=$model_groupbuy_mem->member_list($team);
            foreach ($memList as $key=>$val){
                $memList[$key]['groupbuy_id']=$team_info['groupbuy_id'];
                $memList[$key]['goods_id']=$group_goods['goods_id'];
                $memList[$key]['start_time']=date('Y-m-d H:i:s',$val['start_time']);
                $memList[$key]['end_time']=date('Y-m-d H:i:s',$val['end_time']);
                $memList[$key]['remain_times'] = $this->get_timediff($val['end_time'],$val['start_time']);
                $memList[$key]['member_name']=substr_replace($val['member_name'],"****",3,4);
            }
            
            //查找所有拼图
            $groupbuy_team_model=Model('groupbuy_team');
            $condition_template['groupbuy_id']=$con['groupbuy_id']=$team_info['groupbuy_id'];
            $con['end_time']=array('gt',TIMESTAMP);
            $con['pay_status']=array('gt',0);
            $con['team_id']=array('neq',$team['team_id']);
            $limit=2;
            $team_list=$groupbuy_team_model->get_team_list($limit,$con);
            //查找用户头像
            
            //$member_info['avatar'] = getMemberAvatarForFilePath($this->member_info['member_avatar']);
            $member_model=Model('member');
            
            foreach ($team_list as $key_team =>$val_team){
                $team_list[$key_team]['lead_name']=substr_replace($val_team['lead_name'],"****",3,4);
                $member_id=$team_list[$key_team]['lead_id'];
                $member_info=$member_model->getMemberInfoByID($member_id);
                if (!empty($member_info['member_avatar'])){
                    $team_list[$key_team]['lead_avatar']=getMemberAvatarForFilePath($member_info['member_avatar']);
                }else{                 
                    $team_list[$key_team]['lead_avatar']=UPLOAD_SITE_URL.'/'.ATTACH_AVATAR.'/default_avatar.png';
                }
            
                $team_list[$key_team]['remain_times'] = $this->get_timediff($val_team['end_time'],$val_team['start_time']);
            }
            $group['team_list']=$team_list;
            $group['group_goods']=$group_goods;
            $group['memList']=$memList;
        }
        
        output_data(array('group'=>$group));
    
    }
    
    protected function get_timediff($begin_time,$end_time){
        if($begin_time < $end_time){
            $starttime = $begin_time;
            $endtime = $end_time;
        }else{
            $starttime = $end_time;
            $endtime = $begin_time;
        }
        //计算天数
        $timediff = $endtime-$starttime;
        $days = intval($timediff/86400);
        //计算小时数
        $remain = $timediff%86400;
        $hours = intval($remain/3600);
        //计算分钟数
        $remain = $remain%3600;
        $mins = intval($remain/60);
        //计算秒数
        $secs = $remain%60;
        
        return "{$hours}:{$mins}:{$secs}";
    }
}